<template>
  <div>
    <el-form :inline="true" :model="formInline" class="demo-form-inline">
      <el-button type="primary" @click="dialogFormVisible = true"
        >新增</el-button
      >

      <!-- 弹出框 -->
      <el-dialog title="新增图标" :visible.sync="dialogFormVisible">
        <el-upload
          class="avatar-uploader"
          action="api/navs/upload"
          :show-file-list="false"
          :on-success="handleAvatarSuccess"
          :before-upload="beforeAvatarUpload"
        >
          <img v-if="imageUrl" :src="imageUrl" class="avatar" />
          <i v-else class="el-icon-plus avatar-uploader-icon"></i>
        </el-upload>
        <el-form :model="form">
          <el-form-item label="名称" :label-width="formLabelWidth">
            <el-input v-model="form.name" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="创建时间" :label-width="formLabelWidth">
            <el-input v-model="form.createTime" autocomplete="off"></el-input>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogFormVisible = false">取 消</el-button>
          <el-button
            type="primary"
            @click="(dialogFormVisible = false), add(form)"
            >确 定</el-button
          >
        </div>
      </el-dialog>

      <el-form-item>
        <el-select v-model="formInline.region" placeholder="查询区域">
          <el-option label="名称" value="mingcheng"></el-option>
          <el-option label="创建时间" value="chuangjianshijian"></el-option>
        </el-select>
        <el-form-item>
          <el-input v-model="formInline.user" placeholder="查询内容"></el-input>
        </el-form-item>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSubmit">查询</el-button>
      </el-form-item>
    </el-form>
    <el-table :data="navs" border style="width: 100%">
      <el-table-column label="图标">
        <template slot-scope="scope">
          <el-image
            style="widt: 100px; height: 50px"
            :src="'/api/images/' + scope.row.icon"
            fit=""
          ></el-image>
        </template>
      </el-table-column>
      <el-table-column prop="icon" label="图标链接"> </el-table-column>
      <el-table-column prop="name" label="名称"></el-table-column>
      <el-table-column prop="createTime" label="创建时间"> </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button
            size="mini"
            @click="
              handleEdit(scope.$index, scope.row), (dialogFormVisible1 = true)
            "
            >编辑</el-button
          >

          <el-dialog title="修改图标" :visible.sync="dialogFormVisible1">
            <el-upload
              class="avatar-uploader"
              action="api/navs/upload"
              :show-file-list="false"
              :on-success="handleAvatarSuccess"
              :before-upload="beforeAvatarUpload"
            >
              <img v-if="imageUrl" :src="imageUrl" class="avatar" />
              <i v-else class="el-icon-plus avatar-uploader-icon"></i>
            </el-upload>
            <el-form :model="form1">
              <el-form-item label="名称" :label-width="formLabelWidth">
                <el-input v-model="form1.name" autocomplete="off"></el-input>
              </el-form-item>
              <el-form-item label="创建时间" :label-width="formLabelWidth">
                <el-input
                  v-model="form1.createTime"
                  autocomplete="off"
                ></el-input>
              </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
              <el-button @click="dialogFormVisible1 = false">取 消</el-button>
              <el-button
                type="primary"
                @click="(dialogFormVisible1 = false), gai(form1)"
                >确 定</el-button
              >
            </div>
          </el-dialog>
          <el-button
            size="mini"
            type="danger"
            @click="handleDelete(scope.$index, scope.row)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script>
// import axios from "axios";
// import images from '../../../backserver/public/images'
import { getAllNavs, deleteNavs, addTB, gaiTB } from "@/services/navServices";
// console.log("--------"images);
export default {
  methods: {
    handleAvatarSuccess(res, file) {
      console.log(res);
      console.log(file);
      this.imageUrl = URL.createObjectURL(file.raw);
      console.log(this.imageUrl);
      console.log(file);
      this.form = {
        icon: res.data,
      };
    },
    beforeAvatarUpload(file) {
      const isJPG = file.type === "image/png";
      const isLt2M = file.size / 1024 / 1024 < 2;

      if (!isJPG) {
        this.$message.error("上传头像图片只能是 JPG 格式!");
      }
      if (!isLt2M) {
        this.$message.error("上传头像图片大小不能超过 2MB!");
      }
      return isJPG && isLt2M;
    },

    // 修改
    handleEdit(index, row) {
      console.log(index, row);
      this.form1 = {
        _id: row._id,
        name: row.name,
        createTime: row.createTime,
      };
    },
    async gai(form1) {
      console.log(form1);
      let resp = await gaiTB(form1);
      console.log(resp);
      this.inquire();
    },
    // 删除
    async handleDelete(index, row) {
      console.log(index, row);
      await deleteNavs(row._id);
      this.inquire();
      alert("删除成功");
    },
    // 查询
    async onSubmit() {
      console.log("submit!");
      if (this.formInline.region == "mingcheng") {
        let resp = await getAllNavs();
        console.log(resp);
        console.log(this.formInline.user);
        this.navs = resp.rows.filter(
          (item) => item.name == this.formInline.user
        );
        console.log(this.navs);
      } else if (this.formInline.region == "chuangjianshijian") {
        let resp = await getAllNavs();
        this.navs = resp.rows.filter(
          (item) => item.createTime == this.formInline.user
        );
        console.log(this.navs);
      }
    },
    // 新增
    async add(form) {
      console.log(form);
      let resp = await addTB(form);
      console.log(resp);
        this.inquire();
    },
    async inquire() {
      let resp = await getAllNavs();
      this.navs = resp.rows;
      console.log(this.navs);
    },
  },
  data() {
    return {
      imageUrl: "",
      dialogVisible: false,
      navs: [],
      formInline: {
        user: "",
        region: "",
      },
      dialogFormVisible: false,
      dialogFormVisible1: false,
      form: {
        icon: "",
        name: "",
        createTime: "",
      },
      form1: {
        _id: "",
        icon: "",
        name: "",
        createTime: "",
      },
      formLabelWidth: "120px",
    };
  },
  async created() {
    this.inquire();
  },
};
</script>

<style>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>